package com.example.thymeleaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class URLController {

    @GetMapping("/testURL")
    public ModelAndView testURL() {
        ModelAndView modelAndView = new ModelAndView("/URLTest");

        modelAndView.addObject("nameAttr", "用于测试参数传递的姓名");

        modelAndView.addObject("ageAttr", 20);

        return modelAndView;
    }

    /**
     * 普通的传递参数
     *
     * @param userName
     * @param age
     * @return
     */
    @GetMapping("/transmitParameters")
    public ModelAndView transmitParameters(String userName, int age) {
        System.out.println("userName = " + userName);
        System.out.println("age = " + age);
        /**  不可以通过controller跳转到静态资源
         * 无论是 return "/index";
         *      return new ModelAndView("/index");
         *      都不可以跳转到 resources/static/index.html
         * 因为thymeleaf有默认的视图解析器，前缀是 classpath:/templates/ 所以只能跳转到 该目录下
         * 默认的后缀是.html
         */
        return new ModelAndView("/success");
    }

    /**
     * Thymeleaf中restful格式的参数传递
     *
     * @param studentId
     * @param studentName
     * @return
     */
    @GetMapping("/transmitParametersRestful/{studentId}/{studentName}")
    public ModelAndView transmitParametersRestful(@PathVariable int studentId, @PathVariable String studentName) {
        System.out.println("studentId = " + studentId);

        System.out.println("studentName = " + studentName);

        return new ModelAndView("/success");
    }


    /**
     * 混合参数传递
     * restful方式和传统url方式混合
     *
     * @param itemName
     * @param number
     * @return
     */
    @GetMapping("/mixParameters/{itemName}")
    public ModelAndView mixParameters(@PathVariable String itemName, int number) {
        System.out.println("itemName = " + itemName);
        System.out.println("number = " + number);
        return new ModelAndView("/success");
    }
}
